package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;

@Cmd(code = CrossProtocol.C2_CROSS_ACTIVITY_USER_RANK, desc = "获取个人排名")
public class GetCrossActivityUserRankCmd extends CrossCommand {
    private int activityId;
    private int conditionType;
    private long userId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankProto.CrossActivityUserRankReqMsg req = RankProto.CrossActivityUserRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = req.getActivityId();
        if (req.hasConditionType()) {
            conditionType = req.getConditionType();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        int myRank = -1;
        String myValue = null;
        String myParam = null;
        if (rankListModel != null) {
            myRank = rankListModel.getMyRank(userId, serverId);
            CrossUserRank myRankInfo = rankListModel.getMyRankInfo(userId, serverId);
            if (myRankInfo != null) {
                myValue = myRankInfo.getValue().toString();
                myParam = myRankInfo.getParam();
            }
        }
        RankProto.CrossActivityUserRankRespMsg.Builder msg = RankProto.CrossActivityUserRankRespMsg.newBuilder();
        msg.setRank(myRank);
        if (myValue != null && myParam != null) {
            msg.setValue(myValue);
            msg.setParam(myParam);
        }
        YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_CROSS_ACTIVITY_USER_RANK, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
